//
//  ManageViewController.m
//  SG
//
//  Created by FISH on 2018/4/18.
//  Copyright © 2018年 yimindai. All rights reserved.
//

#import "ManageViewController.h"
#import "ManageCell.h"
@interface ManageViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIView *NaView;

@property (weak, nonatomic) IBOutlet UIButton *rightBtn;

@property (weak, nonatomic) IBOutlet UILabel *contentLab;

@property (weak, nonatomic) IBOutlet UIView *shufflingView;

@property (weak, nonatomic) IBOutlet UIView *buttonView;

@property (weak, nonatomic) IBOutlet UITableView *TableView;

@property (strong, nonatomic) UIButton *selectedBtn;

@property (strong, nonatomic) NSMutableArray *dataSource;
@end

@implementation ManageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = KWhiteColor;

    [self createDataSource];
    
    [self createManageViewControllerUI];
}

-(void)createDataSource
{
    _dataSource = [NSMutableArray array];
    for(int i=0;i<10;i++)
    {
        [_dataSource addObject:StringFormat(@"数据%d",i)];
    }
}

-(void)createManageViewControllerUI
{
    [self createNaViewUI];
    
    [self createShufflingViewUI];
    
    [self createButtonView];
    
    [self createTableView];
}

#pragma createNaViewUI
- (void)createNaViewUI
{
    _NaView.frame = CGRectMake(0, 0, kScreenWidth, kNavHeight);
    _rightBtn.frame = CGRectMake(kScreenWidth-40, kNavY+(44-30)/2, 30, 30);
    _contentLab.frame = CGRectMake(40, kNavY+(44-30)/2, kScreenWidth-80, 30);
    _rightBtn.titleLabel.font = FONTONE;
    [_rightBtn addTarget:self action:@selector(toManageViewControllerMessage) forControlEvents:UIControlEventTouchUpInside];
    _contentLab.font = FONTTHRE;
    _contentLab.text = @"需求管理";
    
    UILabel *rightBtn_label = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 20, 20)];
    rightBtn_label.backgroundColor = KWhiteColor;
    rightBtn_label.font = FONT12;
    rightBtn_label.textAlignment = NSTextAlignmentCenter;
    rightBtn_label.text = @"1";
    [_rightBtn addSubview:rightBtn_label];
}

-(void)createShufflingViewUI
{
    _shufflingView.frame = CGRectMake(0, _NaView.frame.origin.y+_NaView.frame.size.height, kScreenWidth, kRealValue(100));
    

}

-(void)createButtonView
{
    _buttonView.frame = CGRectMake(0, _shufflingView.frame.origin.y+_shufflingView.frame.size.height, kScreenWidth, kRealValue(50));
    NSArray *segmentButtonArray = @[@"全部",@"未成交",@"已成交",@"已过期"];
    int buttonInterval = (kScreenWidth-20-20*segmentButtonArray.count)/segmentButtonArray.count;
    
    for(int i=0;i<segmentButtonArray.count;i++)
    {
        UIButton *segmentButton = [[UIButton alloc]initWithFrame:CGRectMake(10+(buttonInterval+20)*i, (_buttonView.frame.size.height-30)/2, buttonInterval, 30)];
        [segmentButton setTitle:StringFormat(@"%@",segmentButtonArray[i]) forState:UIControlStateNormal];
        segmentButton.titleLabel.font = FONT14;
        segmentButton.tag = i;
        if(i == 0){
            [segmentButton setTitleColor:KWhiteColor forState:UIControlStateNormal];
            segmentButton.backgroundColor = RGB(245, 117, 129);
            _selectedBtn = segmentButton;
        }
        else{
            [segmentButton setTitleColor:KBlackColor forState:UIControlStateNormal];
            segmentButton.backgroundColor = KClearColor;
        }
        [segmentButton addTarget:self action:@selector(tosegmentButton:) forControlEvents:UIControlEventTouchUpInside];
        [_buttonView addSubview:segmentButton];
    }

    
}

-(void)tosegmentButton:(UIButton*)sender
{
    if (_selectedBtn) {
        [_selectedBtn setTitleColor:KBlackColor forState:UIControlStateNormal];
        _selectedBtn.backgroundColor = KClearColor;
    }
    _selectedBtn = sender;
    [_selectedBtn setTitleColor:KWhiteColor forState:UIControlStateNormal];
    _selectedBtn.backgroundColor = RGB(245, 117, 129);
    
}

-(void)createTableView
{
    _TableView.frame = CGRectMake(0, _buttonView.frame.origin.y+_buttonView.frame.size.height, kScreenWidth, kScreenHeight-_buttonView.frame.origin.y-_buttonView.frame.size.height-kTabBarHeight);
    _TableView.delegate = self;
    _TableView.dataSource = self;
    _TableView.bounces = NO;
    _TableView.separatorColor = [UIColor clearColor];
    _TableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        // 加载旧的数据，加载旧的未显示的数据
        [self refsload];
    }];
    _TableView.mj_header = [MJRefreshHeader headerWithRefreshingBlock:^{
        // 加载旧的数据，加载旧的未显示的数据
        [self refsload];
    }];
}
- (void)load
{//加载
    // 主线程执行：
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //获取数据源
        dispatch_async(dispatch_get_main_queue(), ^{
            //回到主线程展示数据
            [self endRefresh];
            if(_dataSource.count){
                _TableView.hidden=NO;
            }
        });
    });
}
-(void)refsload
{//刷新
    [_TableView.mj_header beginRefreshing];
    [_TableView.mj_footer beginRefreshing];
}

//结束刷新
- (void)endRefresh
{
    [_TableView.mj_header endRefreshing];
    [_TableView.mj_footer endRefreshing];
}

#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 240;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataSource.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ManageCell * manCell = [ManageCell cellWithTableView:tableView];

    manCell.selectionStyle = UITableViewCellSelectionStyleNone;//不允许点击
    return manCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];//不允许点击
}

-(void)toManageViewControllerMessage
{
    DLog(@"消息");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
